﻿namespace AC.ExtendedRenderer.Toolkit.Charting
{
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Runtime.InteropServices;

    public class HItems
    {
        private ArrayList arrItem = new ArrayList();
        private double dMaximumValue = 0.0;

        public void Add(double dValue, string strLabel, Color colorBar)
        {
            this.AddItem(dValue, strLabel, colorBar);
        }

        protected void AddItem(HBarData item)
        {
            this.arrItem.Add(item);
            if (this.arrItem.Count == 1)
            {
                this.dMaximumValue = item.Value;
            }
            else if (this.dMaximumValue < item.Value)
            {
                this.dMaximumValue = item.Value;
            }
        }

        protected void AddItem(double dValue, string strLabel, Color colorBar)
        {
            HBarData item = new HBarData(dValue, strLabel, colorBar);
            this.AddItem(item);
        }

        protected void CalculteMaximum()
        {
            if (this.arrItem.Count > 0)
            {
                this.dMaximumValue = ((HBarData) this.arrItem[0]).Value;
                for (int i = 0; i < this.arrItem.Count; i++)
                {
                    if (this.dMaximumValue < ((HBarData) this.arrItem[i]).Value)
                    {
                        this.dMaximumValue = ((HBarData) this.arrItem[i]).Value;
                    }
                }
            }
        }

        public bool GetAt(int nIndex, out HBarData bar)
        {
            bar = null;
            if ((nIndex < 0) || (this.arrItem.Count <= nIndex))
            {
                return false;
            }
            bar = (HBarData) this.arrItem[nIndex];
            return true;
        }

        public bool GetAt(int nIndex, out double dValue)
        {
            dValue = 0.0;
            if ((nIndex < 0) || (this.arrItem.Count <= nIndex))
            {
                return false;
            }
            dValue = ((HBarData) this.arrItem[nIndex]).Value;
            return true;
        }

        protected void InsertItem(int nIndex, HBarData item)
        {
            this.arrItem.Insert(nIndex, item);
            if (this.dMaximumValue < item.Value)
            {
                this.dMaximumValue = item.Value;
            }
        }

        protected void InsertItem(int nIndex, double dValue, string strLabel, Color colorBar)
        {
            HBarData item = new HBarData(dValue, strLabel, colorBar);
            this.InsertItem(nIndex, item);
        }

        public bool RemoveAt(int nIndex)
        {
            if ((nIndex < 0) || (this.arrItem.Count <= nIndex))
            {
                return false;
            }
            this.RemoveItem(nIndex);
            return true;
        }

        protected void RemoveItem(int nIndex)
        {
            double num = ((HBarData) this.arrItem[nIndex]).Value;
            this.arrItem.RemoveAt(nIndex);
            if (num == this.dMaximumValue)
            {
                this.CalculteMaximum();
            }
        }

        public bool SetAt(int nIndex, double dValue, string strLabel, Color colorBar)
        {
            if ((nIndex < 0) || (this.arrItem.Count < nIndex))
            {
                return false;
            }
            if (this.arrItem.Count == nIndex)
            {
                this.AddItem(dValue, strLabel, colorBar);
                return true;
            }
            this.InsertItem(nIndex, dValue, strLabel, colorBar);
            return true;
        }

        public int Count
        {
            get
            {
                return this.arrItem.Count;
            }
        }

        private ArrayList Items
        {
            get
            {
                return this.arrItem;
            }
            set
            {
                this.arrItem = value;
            }
        }

        public double MaxValue
        {
            get
            {
                return this.dMaximumValue;
            }
        }

        public double TotalValue
        {
            get
            {
                double num = 0.0;
                for (int i = 0; i < this.arrItem.Count; i++)
                {
                    num += ((HBarData) this.arrItem[i]).Value;
                }
                return num;
            }
        }
    }
}

